/* --- ESC control ---*/
// arming of ESC 电调的保险
// 一开始给电调供电时, 信号值必须大于等于1ms 对应的电机会发出蜂鸣声 properly armed
// 如果刚开始信号值大于1ms, 之后信号要先低于1ms, 之后电机才会工作
#include <Servo.h>
// using <Servo.h>, we can easily produce 50Hz PWM signal
Servo ESC;  // create servo object to produce PWM signal to control the ESC

int potValue;         // value from the analog pin
String command = "";  // 接收无线数传数据

void setup() {
  // attach(pin, min pulse width, max pulse width)
  ESC.attach(9, 1000, 2000);
  // 在这里定义好脉冲宽度的范围 1000-2000us(micoseconds)
  // potentiometer connected to pin 9

  Serial.begin(9600);
  Serial.println("Serial monitor starts successfully!");
}

void loop() {
  // potValue = analogRead(A0);
  // potValue = map(potValue, 0, 1023, 0, 180);
  // send signal to ESC
  // 0-180 refer to 1000-2000 defined before


  while (Serial.available() > 0) {
    command += char(Serial.read());
    delay(2);
  }

  for (int i = 1500; i <= 2000; i += 100) {
    ESC.write(i);
    delay(2000);
  }
  for (int i = 2000; i >= 1000; i -= 100) {
    ESC.write(i);
    delay(2000);
  }
  //   delay(2000);
  // ESC.write(500);
  //   delay(2000);
  // ESC.write(1000);
  command = "";
}